package com.banciyuan.bcywebview.biz.main.group.hot;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupBanner;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupBase;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupBig;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupHotAnswer;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupPic;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupWaitForYou;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Team;


/**
 * Created by squall on 2015/8/31.
 */
public class GroupHotAdapter extends BaseAdapter {

    private List<Team> tlist = new ArrayList<>();

    private Context mContext;
    private LayoutInflater inflater;
    private int type;

    public GroupHotAdapter(Context context, List<Team> tlist, int type) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(mContext);
        this.tlist = tlist;
        this.type = type;
    }

    @Override
    public int getViewTypeCount() {
        return 6;
    }

    @Override
    public int getItemViewType(int position) {
        Team team = tlist.get(position);
        if ("banner".equals(team.getType())) {
            return 3;
        } else if ("local_zan".equals(team.getType())) {
            return 4;
        } else if ("local_need_attend".equals(team.getType())) {
            return 5;
        } else {
            if (team.isNeed_attend()) {
                return 0;
            } else if (team.getPosts() != null
                    && !team.getPosts().isEmpty()
                    && !TextUtils.isEmpty(team.getPosts().get(0).getImg_src())) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    @Override
    public int getCount() {
        return tlist.size();
    }

    @Override
    public Object getItem(int i) {
        return tlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewGroupBig viewBig = null;
        ViewGroupPic viewPic = null;
        ViewGroupBase viewGroupBase = null;
        ViewGroupBanner viewGroupBanner = null;
        ViewGroupHotAnswer viewGroupHotAnswer = null;
        ViewGroupWaitForYou viewGroupWaitForYou = null;
        if (view == null) {
            switch (getItemViewType(i)) {
                case 0:
                    view = inflater.inflate(R.layout.group_main_item_big_pic, viewGroup, false);
                    viewBig = new ViewGroupBig(view);
                    view.setTag(viewBig);
                    break;
                case 1:
                    view = inflater.inflate(R.layout.group_main_item, viewGroup, false);
                    viewPic = new ViewGroupPic(view);
                    view.setTag(viewPic);
                    break;
                case 2:
                    view = inflater.inflate(R.layout.group_main_item_without_pic, viewGroup, false);
                    viewGroupBase = new ViewGroupBase(view);
                    view.setTag(viewGroupBase);
                    break;
                case 3:
                    view = inflater.inflate(R.layout.group_main_item_big_pic_header, viewGroup, false);
                    viewGroupBanner = new ViewGroupBanner(view);
                    view.setTag(viewGroupBanner);
                    break;
                case 4:
                    view = inflater.inflate(R.layout.group_hot_answer_item, viewGroup, false);
                    viewGroupHotAnswer = new ViewGroupHotAnswer(view);
                    view.setTag(viewGroupHotAnswer);
                    break;
                case 5:
                    view = inflater.inflate(R.layout.group_wait_you_item, viewGroup, false);
                    viewGroupWaitForYou = new ViewGroupWaitForYou(view);
                    view.setTag(viewGroupWaitForYou);
                    break;
                default:
                    view = new View(mContext);
                    break;
            }
        } else {
            switch (getItemViewType(i)) {
                case 0:
                    viewBig = (ViewGroupBig) view.getTag();
                    break;
                case 1:
                    viewPic = (ViewGroupPic) view.getTag();
                    break;
                case 2:
                    viewGroupBase = (ViewGroupBase) view.getTag();
                    break;
                case 3:
                    viewGroupBanner = (ViewGroupBanner) view.getTag();
                    break;
                case 4:
                    viewGroupHotAnswer = (ViewGroupHotAnswer) view.getTag();
                    break;
                case 5:
                    viewGroupWaitForYou = (ViewGroupWaitForYou) view.getTag();
                    break;
                default:
                    break;
            }
        }
        Team team = tlist.get(i);
        if (getItemViewType(i) == 0) {
            if (viewBig != null) {
                viewBig.renderData(mContext, viewBig, team, type);
            }
        } else if (getItemViewType(i) == 1) {
            if (viewPic != null) {
                viewPic.renderData(mContext, viewPic, team, type);
            }
        } else if (getItemViewType(i) == 2) {
            if (viewGroupBase != null) {
                viewGroupBase.renderData(mContext, viewGroupBase, team, type);
            }
        } else if (getItemViewType(i) == 3) {
            if (viewGroupBanner != null) {
                viewGroupBanner.renderData(mContext, team);
            }
        } else if (getItemViewType(i) == 4) {
            if (viewGroupHotAnswer != null) {
                viewGroupHotAnswer.renderData(mContext, team);
            }
        } else if (getItemViewType(i) == 5) {
            if (viewGroupWaitForYou != null) {
                viewGroupWaitForYou.renderData(mContext, team);
            }
        }
        return view;
    }
}


